//
//  OPenAndCloseTheDoorViewController.m
//  CoreAnimation
//
//  Created by 张国文 on 16/10/28.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "OPenAndCloseTheDoorViewController.h"

@interface OPenAndCloseTheDoorViewController ()

@property (nonatomic, strong) UIView *backgroundView;

@property (nonatomic, strong) CALayer *doorLayer;

@property (nonatomic, strong) UIButton *playButton;

@end

@implementation OPenAndCloseTheDoorViewController

/**
 *  timeOffset一个很有用的功能在于你可以它可以让你手动控制动画进程，通过设置speed为0，可以禁用动画的自动播放，然后来使用timeOffset来来回显示动画序列。这可以使得运用手势来手动控制动画变得很简单。
 */

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.backgroundView = [[UIView alloc] initWithFrame:self.view.frame];
    self.backgroundView.backgroundColor = [UIColor colorWithRed:150 / 255.0 green:150 / 255.0 blue:150 / 255.0 alpha:1];
    [self.view addSubview:_backgroundView];
    
    self.doorLayer = [CALayer layer];
    self.doorLayer.frame = CGRectMake(80, 80, 190, 475);
    self.doorLayer.contents = (__bridge id _Nullable)([UIImage imageWithName:@"door"].CGImage);
    self.doorLayer.anchorPoint = CGPointMake(0, 0.5);
    [self.backgroundView.layer addSublayer:_doorLayer];
    
    CATransform3D transform3D = CATransform3DIdentity;
    transform3D.m34 = - 1.0 / 500.0;
    self.backgroundView.layer.sublayerTransform = transform3D;
    
    self.doorLayer.speed = 0.0;
    
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
    animation.duration = 2.0;
    animation.toValue = @(-M_PI);
    [self.doorLayer addAnimation:animation forKey:@"1"];
    
    // 添加手势，进行手动开门可关门
    UIPanGestureRecognizer *panGes = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    [self.backgroundView addGestureRecognizer:panGes];
    
//    // 开始动画,这个只是测试动画
//    self.playButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    self.playButton.frame = CGRectMake(50, 500, 100, 50);
//    self.playButton.backgroundColor = [UIColor whiteColor];
//    [self.playButton setTitle:@"play" forState:UIControlStateNormal];
//    [self.playButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
//    [self.playButton addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];
//    [self.backgroundView addSubview:_playButton];
}

//- (void)play:(UIButton *)button
//{
//    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
//    animation.duration = 2.0;
//    animation.toValue = @(-M_PI_2);
//    [self.doorLayer addAnimation:animation forKey:@"1"];
//}

- (void)pan:(UIPanGestureRecognizer *)panGes
{
    CGFloat x = [panGes translationInView:self.view].x;
   // NSLog(@"x = %f", x);
    x = x / 200.0f;
    CFTimeInterval timeOffset = self.doorLayer.timeOffset;
    timeOffset = MIN(0.999, MAX(0.0, timeOffset - x));
    self.doorLayer.timeOffset = timeOffset;
    NSLog(@"timeOffset=%f", timeOffset);
    [panGes setTranslation:CGPointZero inView:self.view];
    
}

@end
